﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using PloobsEngine;
using PloobsEngine.Audio;
using PloobsEngine.Cameras;
using PloobsEngine.Commands;
using PloobsEngine.Input;
using PloobsEngine.Light;
using PloobsEngine.Material;
using PloobsEngine.Modelo;
using PloobsEngine.Physics;
using PloobsEngine.Physics.Bepu;
using PloobsEngine.SceneControl;
using PloobsEngine.Utils;
using PloobsEngineDemo3;

namespace Etapa1.Screens
{
    /// <summary>
    /// Sound Screen
    /// </summary>
    public class SoundScreen : SceneScreen
    {
        private BepuPhysicWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;        
        ICamera cam;        

        public SoundScreen(EngineStuff es)
        {
            this.engine = es;
            physicWorld = new BepuPhysicWorld();
            mundo = new BrutalForceWorld(physicWorld);                                    
        }
        
        public override void LoadContent(EngineStuff es)
        {
            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);

            base.LoadContent(es);


            IObject emiterSound; 
            #region Models

            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cubo", "..\\Content\\Textures\\white");
                sm.LoadModelo();
                BoxObject box = new BoxObject(new Vector3(200, 20, 0),1,10,Vector3.One * 5);
                NormalDeferred shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader);
                emiterSound = new SimpleObject(mat, box, sm);
                mundo.AddObject(emiterSound);      

            }

            ///classico cenario de sempre                
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario");
                sm.LoadModelo();
                IPhysicObject pi = new TriangleMeshObject(Vector3.Zero, sm);
                CustomDeferred shader = new CustomDeferred(false, false, false, false);
                shader.SpecularIntensity = 0;
                shader.SpecularPower = 0;
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }

            #endregion

            cam = new CameraFirstPerson(true);
            cam.FarPlane = 3000;

            LightThrowBepu lt = new LightThrowBepu(mundo, engine);

            #region NormalLight
            DirectionalLight ld1 = new DirectionalLight(Vector3.Left, Color.White);
            DirectionalLight ld2 = new DirectionalLight(Vector3.Right, Color.White);
            DirectionalLight ld3 = new DirectionalLight(Vector3.Backward, Color.White);
            DirectionalLight ld4 = new DirectionalLight(Vector3.Forward, Color.White);
            DirectionalLight ld5 = new DirectionalLight(Vector3.Down, Color.White);
            float li = 0.4f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            mundo.AddLight(ld1);
            mundo.AddLight(ld2);
            mundo.AddLight(ld3);
            mundo.AddLight(ld4);
            mundo.AddLight(ld5);
            #endregion

            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);

            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;
            mundo.AddCamera(cam);
            this.World = mundo;

            ///Seta as propriedades globais com relacao ao Som
            ///Escala de Decaimento do Som 3d com relacao a distancia
            EngineStuff.SoundEffectDistanceScale = 10;
            ///Altura global
            ///entre 0 e 1
            EngineStuff.SoundEffectMasterVolume = 1;

            ///Adiciona um emissor 3D de som
            ///Smoke on the Water - Deep Purple
            SoundEmitter3D se = new SoundEmitter3D("..\\Content\\Music\\smoke", Vector3.Zero);
            mundo.AddSoundEmitter(se);
            se.Play();

            ///Adiciona um segundo emissor 3D de som (Desta VEZ atachado em um objeto - CAIXA BRANCA)
            ///movimente a caixa branca atirando bolas nela para ver o funcionamento do som 3D
            ///Soldier Side - System of Down
            SoundEmitter3D se2 = new SoundEmitter3D("..\\Content\\Music\\saved", emiterSound,false,1);
            mundo.AddSoundEmitter(se2);
            se2.Play();


            ///Adiciona sons ao repositorio de sons (eles podem ser usados apenas como sons 2D normais)
            SoundEffectAudioPlayer.AddEffectToRepository("..\\Content\\Music\\bye", "bye", 1, 0, 0, false);
            SoundEffectAudioPlayer.AddEffectToRepository("..\\Content\\Music\\alarm", "alarm", 1, 0, 0, false);


            ///Ao clicar com o botao direito do mouse a funcao mousebuttontesteRight sera chamada
            {
                InputPlaybleMouseBottom ip = new SimpleConcreteMouseBottomInputPlayable(StateKey.PRESS, EntityType.IOBJECT, MouseButtons.RightButton, mousebuttontesteRight);
                BindMouseCommand mm = new BindMouseCommand(ip, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandSyncronous(mm);
            }
            ///Ao clicar com o botao esquerdo do mouse a funcao mousebuttontesteLeft sera chamada
            {
                InputPlaybleMouseBottom ip = new SimpleConcreteMouseBottomInputPlayable(StateKey.PRESS, EntityType.IOBJECT, MouseButtons.LeftButton, mousebuttontesteLeft);
                BindMouseCommand mm = new BindMouseCommand(ip, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandSyncronous(mm);
            }
        }

        public void mousebuttontesteRight(MouseState ms)
        {
            ///Toca musica de BackGround
            ///O XNA recupera as musicas que o Windows Media Player ja tocou
            ///A PRIMEIRA VEZ Q ESTA FUNCOA EH CHAMADA DEMOOOOOORA
            BackGroundAudioPlayer.StartQueue();  
        }

        public void mousebuttontesteLeft(MouseState ms)
        {
            ///Toca uns dos sons cadastrados
            if (StaticRandom.RandomChoice(0.5f))
            {                
                SoundEffectAudioPlayer.PlayEffect("bye");
            }
            else
            {
                SoundEffectAudioPlayer.PlayEffect("alarm");
            }            
        }

    }
}

